/**
 * fileName:  FruitStructureWriter.java
 * author:  lr
 * Company:  Cloudland Studio
 * Createdate:  2014-6-11 下午01:53:41
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-11          lr            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.store.write;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.cloudland.commercial.masterdata.store.DatabaseWrite;
import org.cloudland.commercial.masterdata.store.entity.FruitStructureEntity;
import org.cloudland.jdbc.ibatis.masterdata.entity.PartEntity;
import org.cloudland.jdbc.ibatis.masterdata.handler.PartMapper;
import org.cloudland.util.Randoms;
import org.springframework.stereotype.Service;

/**
 * <pre>
 * 水果结构写实现类
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * 
 * @ClassName FruitStructureWriter
 * @author lr
 * @version %I%, %G%
 * @see
 * @since JDK1.6
 * 
 */
@Service
public class FruitStructureWriter implements
		DatabaseWrite<FruitStructureEntity> {

	@Resource(name = "partMapper")
	private PartMapper partMapper;

	public void save(FruitStructureEntity entity) {

		PartEntity daoEntity = new PartEntity();
		String uuid = Randoms.uuid();
		daoEntity.setPartId(uuid);
		daoEntity.setFruitId(entity.getFruitId());
		daoEntity.setSiteDescribe(entity.getSiteDescribe());
		daoEntity.setSiteName(entity.getSiteName());
		daoEntity.setSiteType(entity.getSiteType());
		partMapper.add(daoEntity);
	}

	public void update(FruitStructureEntity entity) {
		PartEntity daoEntity = new PartEntity();
		daoEntity.setPartId(entity.getSiteId());
		daoEntity.setFruitId(entity.getFruitId());
		daoEntity.setSiteDescribe(entity.getSiteDescribe());
		daoEntity.setSiteName(entity.getSiteName());
		daoEntity.setSiteType(entity.getSiteType());
		partMapper.update(daoEntity);

	}

	public void delete(FruitStructureEntity entity) {
		PartEntity daoEntity = new PartEntity();
		daoEntity.setPartId(entity.getSiteId());
		daoEntity.setFruitId(entity.getFruitId());
		daoEntity.setSiteDescribe(entity.getSiteDescribe());
		daoEntity.setSiteName(entity.getSiteName());
		daoEntity.setSiteType(entity.getSiteType());
		partMapper.delete(daoEntity);
	}
	
	public void deleteByFruitId(String fruitId) {
		partMapper.deleteByFruitId(fruitId);
	}

	/**
	 * 
	 * <pre>
	 * 批量增加水果组成结构
	 * </pre>
	 * 
	 * @param entitys
	 * @return
	 */
	public Integer addBatchFruitStructureInfo(List<FruitStructureEntity> entitys) {
		int rowLine = 0;
		if (null != entitys && entitys.size() > 0) {
			List<PartEntity> daoEntitys = new ArrayList<PartEntity>(
					entitys.size());
			PartEntity daoEntity = null;
			String uuid = null;
			for (FruitStructureEntity entity : entitys) {
				uuid = Randoms.uuid();
				entity.setSiteId(uuid);
				daoEntity = new PartEntity(entity.getSiteId(),
						entity.getFruitId(), entity.getSiteName(),
						entity.getSiteDescribe(), entity.getSiteType());
				daoEntitys.add(daoEntity);
			}
			rowLine = partMapper.addBatch(daoEntitys);
		}
		return rowLine;

	}

}
